package gmail

import "gopkg.in/gomail.v2"

type Mail struct {
	Mailer *gomail.Dialer
	Msg    *gomail.Message
}

// 创建mail
func NewMail(host string, port int, username, password string) *Mail {
	return &Mail{
		Mailer: gomail.NewPlainDialer(host, port, username, password),
		Msg:    gomail.NewMessage(),
	}
}

// 设置发送信息
func (m *Mail) SetParams(header map[string]string, contentType string, content string) {
	for k, v := range header {
		m.Msg.SetHeader(k, v)
	}

	m.Msg.SetBody(contentType, content)
}

// 发送
func (m *Mail) Send() error {
	if err := m.Mailer.DialAndSend(m.Msg); err != nil {
		return err
	}
	return nil
}
